package menuService

import (
	"gitee.com/yangguoliang001/go-gin-vue3/QO"
	"gitee.com/yangguoliang001/go-gin-vue3/pkg/response"
	"gitee.com/yangguoliang001/go-gin-vue3/util/code"
	"gitee.com/yangguoliang001/go-gin-vue3/vo"
	"github.com/gin-gonic/gin"
)

func (m menuService) QueryMenuList(c *gin.Context) (menuList []vo.MenuItemVO, err error) {
	menuList, err = m.menuDao.QueryMenuList()
	if err != nil {
		response.Fail(c, code.MenuListError, code.Text(code.MenuListError), gin.H{"realErr": err})
		return nil, err
	}
	return menuList, nil
}

func (m menuService) QueryMenuItemDetail(c *gin.Context, id string) (menuItem vo.MenuItemVO, err error) {
	menuItem, err = m.menuDao.QueryMenuItemDetail(id)
	if err != nil {
		response.Fail(c, code.MenuDetailError, code.Text(code.AdminDetailError), gin.H{"realErr": err})
		return vo.MenuItemVO{}, err
	}
	return menuItem, nil
}

func (m menuService) EditMenu(c *gin.Context, editReq QO.EditReq) (err error) {
	err = m.menuDao.EditMenu(editReq)
	if err != nil {
		if editReq.Id == -1 {
			response.Fail(c, code.MenuCreateError, code.Text(code.MenuCreateError), gin.H{"realErr": err})
			return err
		}
		response.Fail(c, code.MenuUpdateError, code.Text(code.MenuUpdateError), gin.H{"realErr": err})
		return err
	}
	return nil
}

func (m menuService) UpdateMenuUsed(c *gin.Context, usedReq QO.MenuUsedReq) (err error) {
	err = m.menuDao.UpdateMenuUsed(usedReq)
	if err != nil {
		response.Fail(c, code.MenuUpdateError, code.Text(code.MenuUpdateError), gin.H{"realErr": err})
		return err
	}
	return nil
}

func (m menuService) UpdateMenuSort(c *gin.Context, sortReq QO.MenuSortReq) (err error) {
	err = m.menuDao.UpdateMenuSort(sortReq)
	if err != nil {
		response.Fail(c, code.MenuUpdateError, code.Text(code.MenuUpdateError), gin.H{"realErr": err})
		return err
	}
	return nil
}

func (m menuService) DeleteMenu(c *gin.Context, id int32) (err error) {
	err = m.menuDao.DeleteMenu(id)
	if err != nil {
		response.Fail(c, code.MenuDeleteError, code.Text(code.MenuDeleteError), gin.H{"realErr": err})
		return err
	}
	return nil
}
